<template>
  <div class="record-form">
    <div class="type-selector">
      <button
        class="type-btn"
        :class="{ active: form.type === 'expense' }"
        @click="form.type = 'expense'"
      >
        支出
      </button>
      <button
        class="type-btn"
        :class="{ active: form.type === 'income' }"
        @click="form.type = 'income'"
      >
        收入
      </button>
    </div>

    <div class="amount-input">
      <input
        type="number"
        v-model.number="form.amount"
        placeholder="0.00"
        step="0.01"
      >
    </div>

    <div class="form-group">
      <label>分类</label>
      <div class="category-grid">
        <div
          v-for="cat in currentCategories"
          :key="cat.id"
          class="category-item"
          :class="{ active: form.category === cat.id }"
          @click="form.category = cat.id"
        >
          <span class="category-icon">{{ cat.icon }}</span>
          <span class="category-name">{{ cat.name }}</span>
        </div>
      </div>
    </div>

    <div class="form-group">
      <label>备注</label>
      <input
        type="text"
        v-model="form.note"
        placeholder="添加备注（可选）"
        class="note-input"
      >
    </div>

    <div class="form-group">
      <label>日期</label>
      <input
        type="date"
        v-model="form.date"
        class="date-input"
      >
    </div>

    <button class="submit-btn" @click="handleSubmit">
      记一笔
    </button>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { categories } from '@/utils/categories'

const emit = defineEmits(['recordAdded'])

const form = ref({
  type: 'expense',
  amount: '',
  category: '',
  note: '',
  date: new Date().toISOString().split('T')[0]
})

// 当前类型的分类列表
const currentCategories = computed(() => {
  return categories[form.value.type]
})

// 切换类型时重置分类
watch(() => form.value.type, () => {
  form.value.category = ''
})

// 提交表单
const handleSubmit = () => {
  if (!form.value.amount || form.value.amount <= 0) {
    alert('请输入有效的金额！')
    return
  }

  if (!form.value.category) {
    alert('请选择分类！')
    return
  }

  const record = {
    id: Date.now(),
    ...form.value,
    amount: Number(form.value.amount)
  }

  emit('recordAdded', record)

  // 重置表单
  form.value.amount = ''
  form.value.category = ''
  form.value.note = ''
  form.value.date = new Date().toISOString().split('T')[0]
}
</script>

<style scoped>
.record-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.type-selector {
  display: flex;
  gap: 12px;
}

.type-btn {
  flex: 1;
  padding: 14px;
  border: 2px solid var(--border-color);
  background: white;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.type-btn:hover {
  border-color: var(--primary-color);
}

.type-btn.active {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.amount-input input {
  width: 100%;
  padding: 20px;
  font-size: 2rem;
  font-weight: bold;
  border: 2px solid var(--border-color);
  border-radius: 10px;
  text-align: center;
  transition: all 0.3s;
}

.amount-input input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.form-group label {
  font-weight: 600;
  color: var(--text-primary);
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 14px 8px;
  border: 2px solid var(--border-color);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s;
}

.category-item:hover {
  border-color: var(--primary-color);
  transform: translateY(-2px);
}

.category-item.active {
  border-color: var(--primary-color);
  background: rgba(99, 102, 241, 0.1);
}

.category-icon {
  font-size: 1.8rem;
}

.category-name {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.note-input,
.date-input {
  padding: 12px;
  border: 2px solid var(--border-color);
  border-radius: 10px;
  font-size: 1rem;
  transition: all 0.3s;
}

.note-input:focus,
.date-input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.submit-btn {
  padding: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: var(--shadow);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.submit-btn:active {
  transform: translateY(0);
}

@media (max-width: 640px) {
  .category-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .amount-input input {
    font-size: 1.5rem;
  }
}
</style>
